//
//  WBHomeViewController.swift
//  weibo
//
//  Created by 长城 on 16/12/8.
//  Copyright © 2016年 dhsr. All rights reserved.
//

import UIKit
private let cellID = "cellId"
class WBHomeViewController: WBBaseViewController {

    //列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    
    //微博数据组
    //lazy var statusList = [String]()
    
    /// 加载数据
    override func loaddata() {
       
        //print("准备刷新，最后一条数据\(self.listViewModel.statusList.last?.text)")
        listViewModel.loadStatus (pullup: self.isPullUp){ (isSuccess) in
            
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复刷新标记
            self.isPullUp = false
            self.tableview?.reloadData()

        }
    }
    /// 显示好友
    func showFriends(){
        print(#function)
        
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - 表格数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1 取cell
        let cell = tableview?.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        //2 设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3 返回cell
        return cell!
    }
}
// MARK: - 设置界面
extension WBHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableview?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
    /// 重写父类的方法
//    override func setupUI() {
//        super.setupUI()
//        
        //设置导航栏按钮
        
        //无法高亮
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
//        let btn = UIButton(type: .system)
//        btn.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
//        btn.setTitle("好友", for: .normal)
//        btn.setTitleColor(#colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), for: .highlighted)
//        btn.contentHorizontalAlignment = .left
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        
    }
    

